﻿import "System.HTML.ss";
import "System.Math.ss";

public namespace System.HTML.Animation
{	
	public class LinearAnimation
	{
		private num id = 0;
		private num step;
		private num steps;
		private num stepDuration;
		private num from;
		private num to;
		private string unit = "px";
		
		public ctor( num from, num to, num duration )
		{
			this.steps = System.Math.Ceil(duration / 50);
			this.stepDuration = 50;
			this.from = from;
			this.to = to;
		}
		
		public void Start()
		{
			this.Stop();
			this.step = 0;
			this.id = StartIntervalTimer( this.OnAnimation, this.stepDuration );
		}
		
		public void Stop()
		{
			if ( this.id != 0 )
			{
				StopIntervalTimer( this.id );
			}
			this.id = 0;
		}
		
		public event string PositionString;
		
		private void OnAnimation()
		{
			this.step++;
			if ( this.step == this.steps )
			{
				this.PositionString(this.to.ToString() + this.unit);
				this.Stop();
			}
			else
			{
				var diff = (this.to - this.from) / this.steps;
				this.PositionString( (this.from + this.step * diff).ToString() + this.unit);
			}				
		}
	}	
}
